/*
RetroLeague - Mobile application for The Retro League Podcast
Copyright (C) 2010-2013 Hugues Johnson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.retroleague;

import java.io.Serializable;
import java.util.Date;
import java.util.regex.Pattern;

import android.text.Html;
import android.text.Spanned;
import android.text.style.ImageSpan;

public class StreamEntry implements Serializable,Comparable<StreamEntry>{
	private static final long serialVersionUID = 2097474236911286185L;

	public enum Sources{Podcast,Facebook,GooglePlus,Twitter,YouTube,HJ,Forums,Tumblr}

	//common properties needed to display in listview
	private String author;
	private Date postedDate;
	private String title;
	private String category;
	private Sources source;
	//properties for HTML landing page		
	private String imageUrl;
	private String content;
	private Spanned contentSpan;
	private String url;
	//properties for entries with downloads
	private String downloadUrl;
	private String downloadFileName;
	private int downloadLength;
	//pattern to find links
	private static String linkPatternString="<[a|A].*?<\\/[a|A]>";
	private static Pattern linkPattern;
	
	public StreamEntry(){
		if(linkPattern==null){
	        linkPattern=Pattern.compile(linkPatternString);
		}		
	}
	
	@Override
	public int compareTo(StreamEntry another) {
		return(this.postedDate.compareTo(another.getPostedDate())*-1);
	}	
	
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public Date getPostedDate() {
		return postedDate;
	}
	public void setPostedDate(Date postedDate) {
		this.postedDate = postedDate;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public Sources getSource() {
		return source;
	}
	public void setSource(Sources source) {
		this.source = source;
	}
	public String getImageUrl() {
		if(this.imageUrl==null){
			Spanned spannedText=this.getContentSpan();
			ImageSpan[] spans=spannedText.getSpans(0,spannedText.length(),ImageSpan.class);			
			if(spans.length>0){
				this.imageUrl=spans[0].getSource();
			}
		}
		return(this.imageUrl);
	}
	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public synchronized Spanned getContentSpan() {
		if(this.contentSpan==null){
				this.contentSpan=Html.fromHtml(this.content);
		}
		return(this.contentSpan);
	}
	public void setContentSpan(Spanned contentSpan) {
		this.contentSpan = contentSpan;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getDownloadUrl() {
		return downloadUrl;
	}
	public void setDownloadUrl(String downloadUrl) {
		this.downloadUrl = downloadUrl;
	}
	public String getDownloadFileName() {
		if((downloadFileName==null)||(downloadFileName.length()<1)){
			downloadFileName=this.getDownloadUrl();
				int startIndex=downloadFileName.lastIndexOf("/")+1;
				int endIndex=downloadFileName.lastIndexOf("?");
				if(endIndex>startIndex){
					downloadFileName=downloadFileName.substring(startIndex,endIndex);
				}else{
					downloadFileName=downloadFileName.substring(startIndex);
				}
		}
		return(downloadFileName);
	}
	public int getDownloadLength() {
		return downloadLength;
	}
	public void setDownloadLength(int downloadLength) {
		this.downloadLength = downloadLength;
	}

}